<template>
  <div class="code-mirror-box">
    <textarea ref="textareaCoder"></textarea>
  </div>
</template>

<script>
/**
 * @Version 1.0.0
 * @Author DemoHui
 * @Date 2022/10/05
 */

// 引入全局实例
import _CodeMirror from "codemirror";
// 核心样式
import "codemirror/lib/codemirror.css";
// 引入主题后还需要在 options 中 theme 指定主题才会生效
// import "codemirror/theme/monokai.css";
// import "codemirror/theme/panda-syntax.css";
import "codemirror/theme/solarized.css";

// language
// import "codemirror/mode/javascript/javascript.js";
// import "codemirror/mode/css/css.js";
// import "codemirror/mode/xml/xml.js";
// import "codemirror/mode/clike/clike.js";
// import "codemirror/mode/markdown/markdown.js";
// import "codemirror/mode/python/python.js";
// import "codemirror/mode/r/r.js";
import "codemirror/mode/shell/shell.js";
// import "codemirror/mode/sql/sql.js";
// import "codemirror/mode/swift/swift.js";
// import "codemirror/mode/vue/vue.js";

// active-line
import "codemirror/addon/selection/active-line.js";

// foldGutter
import "codemirror/addon/fold/foldgutter.css";
import "codemirror/addon/fold/brace-fold.js";
import "codemirror/addon/fold/comment-fold.js";
import "codemirror/addon/fold/foldcode.js";
import "codemirror/addon/fold/foldgutter.js";
import "codemirror/addon/fold/indent-fold.js";
import "codemirror/addon/fold/markdown-fold.js";
import "codemirror/addon/fold/xml-fold.js";

// 尝试获取全局实例
const CodeMirror = window.CodeMirror || _CodeMirror;

export default {
  name: "CodeMirror",
  data() {
    return {
      // 内部真实的内容
      code: "",
      // 编辑器实例
      coder: null,
      // 默认配置
      options: {
        mode: "shell", // 默认的语法类型
        // 缩进格式
        tabSize: 2,
        // 主题，对应主题库 JS 需要提前引入
        theme: "solarized",
        // 显示行号
        lineNumbers: true,
        line: true,
        styleActiveLine: true, // 激活行样式
        // 添加折叠
        foldGutter: true,
        gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
      },
    };
  },
  mounted() {
    // 初始化
    this._initialize();
  },
  methods: {
    // 初始化
    _initialize() {
      // 初始化编辑器实例，传入需要被实例化的文本域对象和默认配置
      this.coder = CodeMirror.fromTextArea(
        this.$refs.textareaCoder,
        this.options
      );
      // 监听改变
      this.coder.on("change", (coder) => {
        this.code = coder.getValue();
      });
    },
    // 赋值操作
    setValue(val) {
      this.coder.setValue(val);
    },
    // 刷新编辑器操作
    refresh() {
      let timer = setTimeout(() => {
        clearTimeout(timer);
        this.$nextTick(() => {
          this.coder.refresh();
        });
      }, 100);
    },
  },
};
</script>

<style lang="scss" scoped>
.code-mirror-box {
  font-size: 18px;
}
</style>
